/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panel;

import database.DataEngine;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author daz
 */
public class DatabaseInfoPanel extends Screen {

    private JButton editButton;
    private ActionListener editLisnner;
    private JButton okButton;
    private ActionListener okLisnner;
    private DataEngine dataEngine = DataEngine.getInstance();
    private JLabel databaseIpLabel;
    private JTextField databaseIpInputField;
    private JLabel databaseUserLabel;
    private JTextField databaseUserInputField;
    private JLabel databasePassLabel;
    private JPasswordField databasePassInputField;

    public DatabaseInfoPanel(JFrame frame) {
        super(frame);
    }

    @Override
    void initValue() {
    }

    @Override
    void initComponent() {
        //cp line
        setLayout(null);
        int left = 10;
        int top = 20;
        int height = 20;
        int width = 100;
        int linePadding = 45;


        // database adress  line
        top += linePadding;
        databaseIpLabel = new JLabel("Địa chỉ database :");
        databaseIpLabel.setBounds(left, top, 250, height);
        add(databaseIpLabel);

        databaseIpInputField = new JTextField();
        databaseIpInputField.setBounds(left + 130, top, 150, height);
//        databaseIpInputField.setText("202.92.4.22");
        databaseIpInputField.setText("localhost");
        databaseIpInputField.setEditable(false);
        add(databaseIpInputField);
        // database user line
        top += linePadding;
        databaseUserLabel = new JLabel("Database User:");
        databaseUserLabel.setBounds(left, top, 250, height);
        add(databaseUserLabel);

        databaseUserInputField = new JTextField();
        databaseUserInputField.setText("gamepoker");
        databaseUserInputField.setEditable(false);
        databaseUserInputField.setBounds(left + 130, top, 150, height);
        add(databaseUserInputField);
        // database pass line
        top += linePadding;
        databasePassLabel = new JLabel("Database Password:");
        databasePassLabel.setBounds(left, top, 250, height);
        add(databasePassLabel);

        databasePassInputField = new JPasswordField();
//        databasePassInputField.setText("GaMe2o12");
        databasePassInputField.setText("0901hc");
        databasePassInputField.setEditable(false);
        databasePassInputField.setBounds(left + 130, top, 150, height);
        add(databasePassInputField);
        // delete cp button
        top += linePadding;
        editButton = new JButton("Sửa thông tin");
        add(editButton);
        editButton.addActionListener(editLisnner);
        editButton.setBounds(left, top + 10, width, height);
        //
        okButton = new JButton("Confirm");
        add(okButton);
        okButton.addActionListener(okLisnner);
        okButton.setBounds(left + width + 20, top + 10, width, height);
    }

    @Override
    void initListenner() {
        editLisnner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                databaseIpInputField.setEditable(true);
                databaseUserInputField.setEditable(true);
                databasePassInputField.setEditable(true);
            }
        };
        okLisnner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dataEngine.setConfig(databaseIpInputField.getText(), 3306, "gamepoker", databasePassInputField.getText(), "poker_online", "poker_online");
                dataEngine.connectToDabatabase();
                if (dataEngine.isConnected) {
                    showInfo("Kết nối thành công");
                } else {
                    showInfo("Kết nối thất bại");
                }

            }
        };
    }

    private void showInfo(String info) {
        JOptionPane.showMessageDialog(frame,
                info);
    }
}
